<template>
    <div>
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
            <el-tab-pane label="创建新笔记" name="first">
                <div  >
                    <div>
                        <el-form ref="form" :model="note" label-width="80px">
                            <el-form-item label="测试按钮">
                                <el-button class="alt" @click="createBase()">创建数据库</el-button>
                                <el-button class="alt" @click="addOne()">添加一条记录</el-button>
                                <el-button class="alt" @click="queryOne()">查询一条记录</el-button>
                            </el-form-item>
                            <el-form-item label="笔记名称">
                                <el-input v-model="note.name"></el-input>
                            </el-form-item>
                            <el-form-item label="活动区域">
                                <el-select v-model="note.region" placeholder="请选择活动区域">
                                    <el-option label="区域一" value="shanghai"></el-option>
                                    <el-option label="区域二" value="beijing"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="活动时间">
                                <el-col :span="11">
                                    <el-date-picker type="date" placeholder="选择日期" v-model="note.date1"
                                                    style="width: 100%;"></el-date-picker>
                                </el-col>
                                <!--<el-col class="line" :span="2">-</el-col>-->
                                <!--<el-col :span="11">-->
                                    <!--<el-time-picker placeholder="选择时间" v-model="note.date2"-->
                                                    <!--style="width: 100%;"></el-time-picker>-->
                                <!--</el-col>-->
                            </el-form-item>

                            <el-form-item label="特殊资源">
                                <el-radio-group v-model="note.resource">
                                    <el-radio label="线上品牌商赞助"></el-radio>
                                    <el-radio label="线下场地免费"></el-radio>
                                </el-radio-group>
                            </el-form-item>

                            <el-form-item  label="笔记正文">
                                <div class = "editor-container">
                                    <!-- The toolbar will be rendered in this container. -->
                                    <div id="toolbar-container"></div>

                                    <!-- This container will become the editable. -->
                                    <div id="ckEditor" style="padding:25px;">
                                        <p>This is the initial editor content.</p>
                                    </div>
                                </div>
                            </el-form-item>

                            <el-form-item>
                                <el-button type="primary" @click="onSubmit">立即创建</el-button>
                                <el-button>取消</el-button>
                            </el-form-item>
                        </el-form>


                    </div>

                </div>
            </el-tab-pane>
        </el-tabs>

    </div>
</template>


<script>
    import ckEditor from '@ckeditor/ckeditor5-build-decoupled-document';
    import '@ckeditor/ckeditor5-build-decoupled-document/build/translations/zh-cn'
    import sqlite3 from '../../utils/sqlite.js';

    export default {

        name: 'CreateNewNote',
        // components: {UE },
        mounted: function () {
            let that = this;
            ckEditor.create(
                document.querySelector('#ckEditor'),
                {
                    language: 'zh-cn'
                }
            )
            .then(editor => {
                const toolbarContainer = document.querySelector('#toolbar-container');
                toolbarContainer.appendChild(editor.ui.view.toolbar.element);
                that.$data.myEditor = editor;
            })
            .catch(error => {
                console.error(error);
            });
            // ckEditor.pastefromword.con


        },
        methods: {
            onSubmit() {
                console.log('submit!');
                let data = this.myEditor .getData();
                console.log('this is data: \n' + data);




            },
            handleClick(tab, event) {
                console.log(tab, event);
            },
            createBase() {

                let dataDeal = function (objects) {
                    for (let i = 0; i < objects.length; ++i) {
                        console.log(objects[i]);
                    }
                }

                let file = "static/data.sqlite3";
                let sqliteDB = new sqlite3.SqliteDB(file);
                console.log("数据库文件已打开 ");

                let queryTableSql = "select * from sqlite_master where type = 'table' and name = 'labels' ";
                sqliteDB.queryData(queryTableSql, dataDeal);


                let createTileTableSql = "create table if not exists tiles(level INTEGER, column INTEGER, row INTEGER, content BLOB);";
                let createLabelTableSql = "create table if not exists labels(level INTEGER, longitude REAL, latitude REAL, content BLOB);";
                sqliteDB.createTable(createTileTableSql);
                sqliteDB.createTable(createLabelTableSql);


                /// insert data.
                // let tileData = [[1, 10, 10], [1, 11, 11], [1, 10, 9], [1, 11, 9]];
                // let insertTileSql = "insert into tiles(level, column, row) values(?, ?, ?)";
                // sqliteDB.insertData(insertTileSql, tileData);


                let querySql = 'select * from tiles where level = 1 and `column` >= 10 and `column` <= 11 and row >= 10 and row <=11';


                console.log(querySql);
                sqliteDB.queryData(querySql, dataDeal);
                sqliteDB.close();

            },
            addOne() {

            },
            queryOne() {

            },


        },
        data() {
            return {
                activeName: 'first',
                testData: {},
                note: {
                    name: '',
                    region: '',
                    date1: '',
                    date2: '',
                    delivery: false,
                    type: [],
                    resource: '',
                    desc: ''
                },
                myEditor : {}
            }


        }
    }
</script>

<style>
    .editor-container {
        background-color:#e8eef7;
        min-height: 500px;
    }
    li ol {
        margin-left:2em ;
    }

    .info{
        border-radius: 10px;
        line-height: 20px;
        padding: 10px;
        margin: 10px;
        background-color: #ffffff;
    }


</style>